package com.centaline.androidsalesblog.activities.entrust;

import android.content.DialogInterface;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.EntrustFragmentAdapter;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.base.BaseFragment;
import com.centaline.androidsalesblog.fragments.entrust.EntrustRentFragment;
import com.centaline.androidsalesblog.fragments.entrust.EntrustSaleFragment;

import java.util.ArrayList;

/**
 * 委托
 * Created by Lihq on 2016/2/25.
 */
public class EntrustActivity extends BaseActivity {
    private ViewPager viewPager;
    private TabLayout tableLayout;

    private EntrustSaleFragment entrustSaleFragment;
    private EntrustRentFragment entrustRentFragment;


    @Override
    protected int setLayoutId() {
        return R.layout.activity_entrust_sale_rent;
    }

    @Override
    protected void findViews() {
        tableLayout = (TabLayout) findViewById(R.id.tl_entrust);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
    }

    @Override
    protected void initViews() {
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ArrayList<String> titleList = new ArrayList<>();
        titleList.add("委托卖房");
        titleList.add("委托出租");

        entrustSaleFragment = new EntrustSaleFragment();
        entrustRentFragment = new EntrustRentFragment();
        ArrayList<BaseFragment> fragmentArrayList = new ArrayList<>();
        fragmentArrayList.add(entrustSaleFragment);
        fragmentArrayList.add(entrustRentFragment);

        tableLayout.setTabMode(TabLayout.MODE_FIXED);
        tableLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        for (int i = 0; i < 2; i++) {
            tableLayout.addTab(tableLayout.newTab().setText(titleList.get(i)));
        }

        EntrustFragmentAdapter entrustFragmentAdapter = new EntrustFragmentAdapter(getSupportFragmentManager(), fragmentArrayList, titleList);
        viewPager.setAdapter(entrustFragmentAdapter);
        tableLayout.setupWithViewPager(viewPager);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            showDialog();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            showDialog();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showDialog() {
        new AlertDialog.Builder(EntrustActivity.this)
                .setMessage("您还有未提交的委托，是否退出？")
                .setPositiveButton("退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                })
                .setNegativeButton("取消", null)
                .create()
                .show();
    }
}
